package com.vinaprime.primeschedule.databaselayer;

import com.vinaprime.primeschedule.entities.VPSnoozeSound;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/** * 
 * @author GiangVH
 * @since 01/08/2013
 *
 */
@SuppressLint("NewApi")
public class VPSnoozeSound_DL {
	private static final String DB_TABLE_NAME = "VP_SNOOZE_SOUND";
	private static final String  DB_TABLE_VPSS_ID = "VPSS_ID";
	private static final String DB_VPSS_FILE_PATH= "VPSS_FILE_PATH";
	private static final String DB_VPSS_ENABLE_FLAG ="VPSS_ENABLE_FLAG";	
	private static final String DB_VPSS_SOUND ="VPSS_SOUND";
	private static final String DB_VPSS_SIZE ="VPSS_SIZE";
	public DatabaseController_DL myDatabaseControllerDL;
	
	public VPSnoozeSound_DL(Context c,String path){
		this.myDatabaseControllerDL = new DatabaseController_DL(c,path);
	}
	
	public static String getDbTableName() {
		return DB_TABLE_NAME;
	}
	
	public static String getDbTableVpssId() {
		return DB_TABLE_VPSS_ID;
	}
	
	public static String getDbVpssFilePath() {
		return DB_VPSS_FILE_PATH;
	}
	
	public static String getDbVpssEnableFlag() {
		return DB_VPSS_ENABLE_FLAG;
	}
	
	public static String getDbvpssSound() {
		return DB_VPSS_SOUND;
	}
	
	public static String getDbVpssSize() {
		return DB_VPSS_SIZE;
	}

	public Cursor getAllSound(){
		Cursor cur = null;
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM "+ DB_TABLE_NAME;
		cur =  db.rawQuery(strQuery, null);
		return cur;
		
	}
	
	public Cursor getAllSoundActived(){
		Cursor cur = null;
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM " + DB_TABLE_NAME + " WHERE " + DB_VPSS_ENABLE_FLAG + " = 'Y'";
		cur =  db.rawQuery(strQuery, null);
		return cur;
		
	}
	public Cursor getNewSnoozeSoundID(){
		Cursor cur = null;
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT MAX(" + DB_TABLE_VPSS_ID + ") AS "+ DB_TABLE_VPSS_ID + " FROM " + DB_TABLE_NAME;
		cur = db.rawQuery(strQuery, null);
		return cur;
	}
	
	public Cursor getSnoozeSoundbyID(int id){
		Cursor cur = null;
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPSS_ID+" = " + id;
		cur = db.rawQuery(strQuery, null);
		return cur;
	}
	
	public void addSnoozeSound(VPSnoozeSound sound){
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put(DB_TABLE_VPSS_ID, sound.getVpssId());
		content.put(DB_VPSS_FILE_PATH, sound.getVpssFilePath());
		content.put(DB_VPSS_ENABLE_FLAG, sound.getVpssEnableFlag());
		content.put(DB_VPSS_SOUND, sound.getVpssSound());
		content.put(DB_VPSS_SIZE, sound.getVpssSize());
		db.insert(DB_TABLE_NAME, null, content);
		db.close();
	}
	
	public int updateSnoozeSound(VPSnoozeSound sound){
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put(DB_TABLE_VPSS_ID, sound.getVpssId());
		content.put(DB_VPSS_FILE_PATH, sound.getVpssFilePath());
		content.put(DB_VPSS_ENABLE_FLAG, sound.getVpssEnableFlag());
		content.put(DB_VPSS_SOUND, sound.getVpssSound());
		content.put(DB_VPSS_SIZE, sound.getVpssSize());
		return db.update(DB_TABLE_NAME, content, DB_TABLE_VPSS_ID + " = ?",new String[] { String.valueOf(sound.getVpssId())} );
	}
	
	public void deleteSnoozeSound(VPSnoozeSound sound){
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, DB_TABLE_VPSS_ID + " =? ", new String []{ String.valueOf(sound.getVpssId())});
		db.close();
	}
	
	public void deleteSnoozeSoundbyId (int id){
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, DB_TABLE_VPSS_ID + " =? ", new String []{ String.valueOf(id)});
		db.close();
	}
	
	public int updateSnoozeSound_EnableFlag(VPSnoozeSound sound){
		SQLiteDatabase db = myDatabaseControllerDL.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put(DB_TABLE_VPSS_ID, sound.getVpssId());		
		content.put(DB_VPSS_ENABLE_FLAG, sound.getVpssEnableFlag());		
		return db.update(DB_TABLE_NAME, content, DB_TABLE_VPSS_ID + " = ?",new String[] { String.valueOf(sound.getVpssId())} );
	}

}
